package com.thitech.sif;

import com.thitech.sif.context.AbsContext;
import com.thitech.sif.context.ApplicationContextFactory;
import com.thitech.sif.context.SocketContext;
import com.thitech.sif.context.WSContext;
import com.thitech.sif.utils.Assert;
import com.thitech.sif.utils.DataMap;

/**
 * 服务执行器抽象实现
 * @author shisheng.lian
 *
 */
public abstract class AbsExecutor implements Executor {
	private String serviceId;
	private String ticket;
	private String businessId;
	private DataMap extDataMap;
	private DataMap serviceKeyDataMap;

	public AbsExecutor(String serviceId, String ticket, String businessId) {
		this.serviceId = serviceId;
		this.ticket = ticket;
		this.businessId = businessId;
	}
	
	public AbsExecutor() {}

	@Override
	public void setServiceId(String serviceId) {
		this.serviceId = serviceId;
	}

	@Override
	public String getServiceId() {
		return this.serviceId;
	}

	@Override
	public void setTicket(String ticket) {
		this.ticket = ticket;
	}

	@Override
	public String getTicket() {
		return this.ticket;
	}

	@Override
	public void setBusinessId(String businessId) {
		this.businessId = businessId;
	}

	@Override
	public String getBusinessId() {
		return this.businessId;
	}

	@Override
	public DataMap getExtDataMap() {
		if (null == this.extDataMap) {
			this.extDataMap = new DataMap();
		}
		return this.extDataMap;
	}

	@Override
	public DataMap getServiceKeyDataMap() {
		if (null == this.serviceKeyDataMap) {
			this.serviceKeyDataMap = new DataMap();
		}
		return this.serviceKeyDataMap;
	}

	@Override
	public AbsContext getContext() {
		return ApplicationContextFactory.getInstances().getContext(getServiceType() + "_" + getServiceId());
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Service Id=").append(getServiceId()).append(";");
		sb.append("Business Id=").append(getBusinessId()).append(";");
		sb.append("Ticket=").append(getTicket()).append(";");
		sb.append("ExtDataMap=").append(getExtDataMap().toString()).append(";");
		sb.append("ServiceKeyDataMap=").append(getServiceKeyDataMap().toString());
		return sb.toString();
	}

	/**
	 * 检查请求入参是否合法
	 */
	public void validateInputParam(AbsOutput output, Helper helper) {
		Assert.nullObject(output, "WSBaseOutput is null!");
		Assert.nullObject(helper, "WSHelper is null!");
		
		Assert.nullString(getServiceId(), "Service id is null!");
		Assert.nullString(getBusinessId(), "Business id is null!");
		Assert.nullString(getTicket(), "Ticket is null!");
		
		AbsContext ctx = getContext();
		Assert.nullObject(ctx, "Application Context is null!");
		Assert.nullString(ctx.getSystemName(), "System name is null!");
		Assert.nullString(ctx.getServiceName(), "Service name is null!");
		Assert.nullString(ctx.getHost(), "Host is null!");
		Assert.nullString(ctx.getReturnAssertClass(), "Return assert class is null!");
		
		if (ctx instanceof WSContext) {
			WSContext wsCtx = (WSContext) ctx;
			Assert.nullString(wsCtx.getLocatorClass(), "Web Service locator class is null!");
			Assert.nullString(wsCtx.getPortName(), "Web Service port name is null!");
			Assert.nullString(wsCtx.getQueryString(), "Web Service query string is null!");
		} else if (ctx instanceof SocketContext) {
			//暂无
		}
	}
}
